package graphe
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	
	import mx.containers.Canvas;
	import mx.controls.Label;
	import mx.core.UIComponent;
	
	
	public class Node extends Canvas
	{
		public static var RADIUS:int = 20;
		public static var BORDER_CLR_MARK:int     = 0xf40095;
		public static var BORDER_CLR_UNMARK:int   = 0x999999;
		public static var BORDER_CLR_ROLLOVER:int = 0x03a3f0;
		public static var FILL_CLR:int            = 0xffffff;
		
		public var graphIndex:int;
		public var nodePorte:String;
		public var nodeSalle:String;
		public var color:uint;
		public var marked:Boolean;
		public var a_enabled:Boolean;
		
		public function Node(nS:String,nP:String,col:uint,graphIndex:int)
		{
			this.color = col;
			drawNode(this.color);
			drawText();
			//write(nodeName + " " + graphIndex);
			this.nodeSalle = nS;
			this.nodePorte = nP;
			this.graphIndex = graphIndex;
			
			write(nodeSalle +"_"+nodePorte);
			marked = false;
			a_enabled = true;
			
			//freeze(false);
		}
		
		public function freeze(flag:Boolean):void
		{
			if (flag)
			{
				removeEventListener(MouseEvent.MOUSE_OVER, onRollOver);
				removeEventListener(MouseEvent.MOUSE_OUT,  onRollOut);
				removeEventListener(MouseEvent.MOUSE_DOWN, onPress);
			}
			else
			{
				addEventListener(MouseEvent.MOUSE_OVER, onRollOver, false, 1000);
				addEventListener(MouseEvent.MOUSE_OUT,  onRollOut, false, 1000);
				addEventListener(MouseEvent.MOUSE_DOWN, onPress, false, 1000);
			}
		}
		
		public function mark():void
		{
			drawNode(BORDER_CLR_MARK);
		}
		
		public function unmark():void
		{
			marked = false;
			drawNode(BORDER_CLR_UNMARK);
		}
		
		public function write(s:String):void
		{
			var tf:Label = getChildAt(0) as Label;
			tf.text = s;
			tf.x = int(-this.width  / 2);
			tf.y = int(-this.height / 2);
		}
		
		private function onRollOver(e:MouseEvent):void
		{
			if (marked) return;
			drawNode(BORDER_CLR_ROLLOVER);
		}
		
		private function onRollOut(e:MouseEvent):void
		{
			if (marked) return;
			drawNode(BORDER_CLR_UNMARK);
		}
		
		private function onPress(e:MouseEvent):void
		{
			marked = !marked;
			drawNode(marked ? BORDER_CLR_MARK : BORDER_CLR_ROLLOVER);
		}
		
		private function drawNode(clr:uint):void
		{
			graphics.clear();
			graphics.beginFill(FILL_CLR, 1);
			graphics.lineStyle(2, clr, 1);
			graphics.drawCircle(0, 0, RADIUS);
			graphics.endFill();
		}
		
		private function drawText():void
		{
			var tf:Label = new Label();
			tf.name = "counter";
			tf.selectable = false;
			addChild(tf);
		}
		
		override public function toString():String
		{
			return "[NODE, graphIndex " + graphIndex + "- color : "+ color +"]";
		}
	}
}
